package Search;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.BoostingQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import utils.ElasticSearchClient;
import java.util.Map;

/**
 * @author：PZ
 * @Date: 2020/12/11 15 43
 */
public class Search3 {
    ObjectMapper mapper = new ObjectMapper();
    RestHighLevelClient client = ElasticSearchClient.getClient();
    String index = "sms-logs-index";
    String type = "sms-logs-type";
    
    /**
     * bool复合查询
     *
     * @throws Exception
     */
    @Test
    public void boolSearch() throws Exception {
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //北京或者上海
        boolQueryBuilder.should(QueryBuilders.termQuery("province", "北京"));
        boolQueryBuilder.should(QueryBuilders.termQuery("province", "上海"));
        //排除operatorId为2
        boolQueryBuilder.mustNot(QueryBuilders.termQuery("operatorId", "2"));
        //smsContent中包含一群与孩童
        boolQueryBuilder.must(QueryBuilders.matchQuery("smsContent", "一群"));
        boolQueryBuilder.must(QueryBuilders.matchQuery("smsContent", "孩童"));
        builder.query(boolQueryBuilder);
        request.source(builder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        System.out.println(hits.length);
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }
    
    /**
     * boosting查询
     *
     * @throws Exception
     */
    @Test
    public void boostingSearch() throws Exception {
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoostingQueryBuilder boostingQueryBuilder = QueryBuilders.boostingQuery(
                QueryBuilders.matchQuery("smsContent", "孩童"),
                QueryBuilders.matchQuery("smsContent", "希尔曼")
        );
        boostingQueryBuilder.negativeBoost(0.5F);
        builder.query(boostingQueryBuilder);
        request.source(builder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        System.out.println(hits.length);
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }
}
